package com.xuanwei.core.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xuanwei.core.exception.XwException;
import com.xuanwei.core.service.IXwHelpService;
import com.xuanwei.dao.entity.XwHelp;
import com.xuanwei.dao.mapper.XwHelpMapper;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author mjy
 * @since 2019-08-27
 */
@Service
public class XwHelpServiceImpl extends ServiceImpl<XwHelpMapper, XwHelp> implements IXwHelpService {

    @Override
    public void addOrUpdate(XwHelp help) {
        if (help.getId() == null) {
            help.setCreateTime(LocalDateTime.now());
            help.setUpdateTime(LocalDateTime.now());
            save(help);
        } else {
            XwHelp byId = getById(help.getId());
            if (byId == null) {
                throw new XwException("数据不存在");
            }
            help.setUpdateTime(LocalDateTime.now());
            updateById(help);
        }
    }

    @Override
    public void updateStatus(Integer id) {
        XwHelp byId = getById(id);
        if (byId == null) {
            throw new XwException("数据不存在");
        }
        byId.setUpdateTime(LocalDateTime.now());
        byId.setShowStatus(byId.getShowStatus() == 0 ? 1 : 0);
        updateById(byId);
    }

    @Override
    public List<XwHelp> findAll(Integer type) {
        QueryWrapper<XwHelp> wrapper = new QueryWrapper<XwHelp>()

                .orderByAsc("order_by")
                .orderByDesc("create_time");

        if (type != null && type == 0) {
            wrapper.eq("show_status", 0);
        }
        return list(wrapper);
    }
}
